/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.error.view;

import offset.nodes.server.error.controller.ErrorAttribute;
import offset.nodes.server.error.model.ErrorHandler;
import offset.nodes.server.view.FacesUtils;

/**
 * Display error message. In the header, the path will allow to do some meaningful navigation afterwards.
 * 
 * @author Walter Lütgenau
 */
public class ErrorBean {

     String message;
     String title;
     String severity;
     
     public static final String PATTERN_SEPARATOR = "::";

    protected void init() {
        ErrorAttribute att = (ErrorAttribute) FacesUtils.getRequest().getAttribute(ErrorAttribute.ATT_ERROR);
        if (att == null) {
            message = "";
            return;
        }

        message = att.getException().getMessage(FacesUtils.getRequest().getLocale());

        StringBuffer titleBuffer = new StringBuffer();
        message = ErrorHandler.getMessage(message, titleBuffer);
        title = titleBuffer.toString();

        switch(att.getException().getExceptionId().getSeverity()) {
            case Debug:
                severity = "debug";
                break;

            case Info:
                severity = "info";
                break;

            case Warning:
                severity = "warning";
                break;

            case Error:
                severity = "error";
                break;
        }
    }

    public String getMessage() {
        init();
        return message;
    }

    public String getTitle() {
        init();
        return title;
    }

    public String getSeverity() {
        init();
        return severity;
    }

}
